package com.quanyan.stadiumScheduler.task;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.stadiumScheduler.biz.BizGameOrderDao;
import com.quanyan.stadiumScheduler.constants.GameConstants;
import com.quanyan.stadiumScheduler.mapper.DatesportMapper;
import com.quanyan.stadiumScheduler.service.DataReductionService;
import com.quanyan.stadiumScheduler.service.GameEventService;
import com.quanyan.stadiumScheduler.service.GameMQLog;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;

/**
 */
@Component
public class StadiumTaskJob implements JobRunner{

    private static Logger logger = Logger.getLogger(StadiumTaskJob.class);

    @Autowired
    private DataReductionService dataReductionService;

    @Autowired
    BizGameOrderDao gameOrderDao;

    @Autowired
    private GameMQLog gameMQLog;

    @Autowired
    private DatesportMapper datesportMapper;

    @Autowired
    private GameEventService gameEventService;


    @Override
    public Result run(JobContext jobContext) throws Throwable {
        Job job = jobContext.getJob();
        if (job == null) {
            return new Result(Action.EXECUTE_EXCEPTION, "任务不存在");
        }
        logger.info("收到定时任务：" + JSON.toJSONString(job));
        String taskId = job.getTaskId();
        try {
            if (taskId.contains(GameConstants.STADIUM_JOB_ID_CLOSE_ORDER)) {
                gameOrderDao.clearExpireOrder();
                return new Result(Action.EXECUTE_SUCCESS, "--定时扫描关闭订单---task-execute-success----");
            } else if (taskId.contains(GameConstants.STADIUM_JOB_ID_REFRESH_OFFICIAL_GAME)) {
                return new Result(Action.EXECUTE_SUCCESS, "--自动刷新赛事的报名状态(暂时关闭)---task-execute-success----");
            } else if (taskId.contains(GameConstants.STADIUM_JOB_ID_REFRESH_COMPLEMENTE_OFFICIAL_GAME)) {
                return new Result(Action.EXECUTE_SUCCESS, "--官方赛事分数推位记录(暂时关闭)---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_REFRESH_GAME_EVENT_NUM)) {
                dataReductionService.updateGameEventMsg();
                return new Result(Action.EXECUTE_SUCCESS, "--赛事数据汇总---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_GAME_START_REMIND_0_HOUR)) {
                dataReductionService.gameBeforeOneHourEventStartRemind(10);
                return new Result(Action.EXECUTE_SUCCESS, "--比赛开始通知---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_REFRESH_EXCEPTION_MQ)) {
                gameMQLog.operateExceptionMQ();
                return new Result(Action.EXECUTE_SUCCESS, "--开始处理超过6分钟未处理的内部消息---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_REFRESH_INVITE_COUNT)) {
                datesportMapper.updateInviteCount(GameConstants.MAX_INVITE_CNT, new Date());
                return new Result(Action.EXECUTE_SUCCESS, "--邀请次数每天凌晨恢复---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_UPDATE_PERSON_PK_RECORD)) {
                gameEventService.lockScoreAndEndPersonalGameEvents();
                return new Result(Action.EXECUTE_SUCCESS, "--更新个人对抗赛录入比分状态---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_CLOSE_PERSON_PK_EXPIRE)) {
                gameEventService.endPersonalGameEvents();
                return new Result(Action.EXECUTE_SUCCESS, "--关闭比赛开始为报名的比赛---task-execute-success----");
            }else if (taskId.contains(GameConstants.STADIUM_JOB_ID_SYSTEM_CANCEL_CLUB_GAME_ENENTS_EXPIRE)) {
                gameEventService.clubGameEventsSystemCancel();
                return new Result(Action.EXECUTE_SUCCESS, "--系统自动取消超过七天未编排的内部赛---task-execute-success----");
            }else {
                //没有匹配类型，默认提交成功
                return new Result(Action.EXECUTE_SUCCESS, "当前没有可执行的业务逻辑");
            }
        } catch (Exception e) {
            logger.info("Run job failed!", e);
            return new Result(Action.EXECUTE_LATER, "--task-execute-fail---"+e);
        }
    }
}